/** @file    wCore_List.cpp
 *  @author  Alessandro Polo
 *  @version $Id: wCore_List.cpp 2658 2010-06-15 21:45:46Z alex $
 *  @brief
 * File containing methods for the wosh::gui::wCore_List class.
 * The header for this class can be found in wCore_List.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "wCore_List.h"

 #include <core/ObjectAllocator.h>
 #include <framework/gui/GuiUtilities.h>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::wCore_List, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_List )
 WOSH_GUI_MAP(wosh::gui::wCore_List, "wosh::List", 0.0, _static_Map_wCore_List )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

wCore_List::wCore_List( QWidget* parent )
	: QListView(parent), listModel(NULL) {

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	this->listModel = new ListModel_Core_List(this);
	QListView::setModel(this->listModel);
//setSortingEnabled(true);

 }

wCore_List::~wCore_List() {
	if ( this->listModel != NULL )
		delete this->listModel; this->listModel = NULL;
 }


WRESULT wCore_List::loadObject( const wosh::List* object ) {
	return this->listModel->loadObject(object);
 }

WRESULT wCore_List::saveObject( wosh::List* object ) const {
	if ( object == NULL ) return WRET_ERR_PARAM;

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

ListModel_Core_List::ListModel_Core_List(QObject *parent)
	: QAbstractListModel(parent), dataList(NULL) {

 }

ListModel_Core_List::~ListModel_Core_List() {
	if ( this->dataList != NULL )
		delete this->dataList; this->dataList = NULL;
 }

WRESULT ListModel_Core_List::loadObject( const wosh::List* list ) {
	if ( this->dataList != NULL )
		delete this->dataList; this->dataList = NULL;
	if ( list == NULL )
		return WRET_OK;
	this->dataList = list->clone();
	reset();
	return WRET_OK;
 }

WRESULT ListModel_Core_List::acquireObject( wosh::List* list ) {
	if ( this->dataList != NULL )
		delete this->dataList; this->dataList = NULL;
	this->dataList = list;
	reset();
	return WRET_OK;
 }

int ListModel_Core_List::rowCount(const QModelIndex &parent) const {
	(void)parent;
	if ( this->dataList == NULL ) return 0;
	return this->dataList->size();
 }

QVariant ListModel_Core_List::data(const QModelIndex &index, int role) const {
	if ( this->dataList == NULL || !index.isValid() )
		return QVariant();

	if ( role == Qt::DisplayRole ) {
		return GuiUtilities::convertSerializable2QVariant(this->dataList->itemAt(index.row()));
	 }
	else if ( role == Qt::ToolTipRole ) {
		return GuiUtilities::convertSerializable2QVariant(this->dataList->itemAt(index.row()));
	 }
	return QVariant();
 }

bool ListModel_Core_List::setData(const QModelIndex &index, const QVariant &value, int role) {
	if ( this->dataList == NULL ) return false;
	if ( !index.isValid() || role != Qt::EditRole)
		return false;
	(void)value;
//	value = GuiUtilities::convertSerializable2QVariant( this->dataList->indexOf(index.row()) );
	emit dataChanged(index, index);
	return true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


 }; // namespace gui
}; // namespace wosh
